<?php

namespace App\Libraries\AppSocialite;

use App\Libraries\AppSocialite\Contracts\Factory;
use App\Libraries\AppSocialite\Providers\AppleProvider;
use App\Libraries\AppSocialite\Providers\PhoneProvider;
use App\Libraries\AppSocialite\Providers\WeChatProvider;
use App\Models\Setting;
use Illuminate\Support\Manager;
use InvalidArgumentException;

class AppSocialiteManager extends Manager implements Factory
{
    /**
     * Create an instance of the WeChat driver.
     *
     * @return  \App\Libraries\AppSocialite\Providers\AbstractProvider
     */
    public function createWeChatDriver()
    {

        if(Setting::get("app_id") && Setting::get("app_secret")){

            $config = [
                'appid' => Setting::get("app_id"),
                'appsecret' => Setting::get("app_secret"),
            ];
        }else{

            $config = $this->config->get('services.wechat');
        }

        return $this->buildProvider(WeChatProvider::class, $config);
    }

    /**
     * Create an instance of the Apple driver.
     *
     * @return  \App\Libraries\AppSocialite\Providers\AbstractProvider
     */
    public function createAppleDriver()
    {
        return $this->buildProvider(AppleProvider::class, []);
    }

    public function createPhoneDriver()
    {
        return $this->buildProvider(PhoneProvider::class, []);
    }

    /**
     * Build an provider instance.
     *
     * @param   string $provider
     * @param   array  $config
     *
     * @return  \App\Libraries\AppSocialite\Providers\AbstractProvider
     */
    public function buildProvider(string $provider, array $config)
    {
        return new $provider($this->container->make('request'), $config);
    }

    /**
     * Get the default driver name.
     *
     * @return string
     *
     * @throws \InvalidArgumentException
     */
    public function getDefaultDriver()
    {
        throw new InvalidArgumentException('No Socialite driver was specified.');
    }
}
